//
//  BGWenBenCell.m
//  bigMall
//
//  Created by ios on 2016/12/9.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGWenBenCell.h"
#import "BGReadStepTwoModel.h"

@interface BGWenBenCell ()<UITextFieldDelegate>  {
    
    UILabel *titleLab;
    UITextField *textFiled;
    UIButton *isUseBtn;
    
    NSMutableArray *isUseArrM;
    NSMutableArray *attributeArrM;
    
    NSString *_fabuName;
    
}

@end
@implementation BGWenBenCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier default_value:(NSArray *)default_valueArr  fabuName:(NSString *)fabuName {
    
    isUseArrM = [NSMutableArray array];
    attributeArrM = [NSMutableArray array];
    _fabuName = fabuName;
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        BGReadStepTwoModelArrDetail *model = default_valueArr.firstObject;
        
        //修饰线
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        v1.backgroundColor = dockwhite_;
        [self.contentView addSubview:v1];
        
        //标题的
        titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 10, 70, 21)];
        titleLab.font = [UIFont systemFontOfSize:16];
        titleLab.text = self.titleText;
        [self.contentView addSubview:titleLab];
        
        //填写的文本
        textFiled = [[UITextField alloc] initWithFrame:CGRectMake(titleLab.right + 10, 10, 100, 21)];
        textFiled.delegate = self;
        textFiled.font = [UIFont systemFontOfSize:14];
        
        [textFiled addTarget:self action:@selector(change:) forControlEvents:UIControlEventEditingDidEnd];
        textFiled.placeholder = @"请填写";
        textFiled.text = model.value;
        textFiled.userInteractionEnabled = NO;
        [self.contentView addSubview:textFiled];
        [attributeArrM addObject:textFiled];
        
        //可使用按钮
        isUseBtn = [[UIButton alloc] initWithFrame:CGRectMake(textFiled.right + 5, 12.5, 50, 16)];
        [isUseBtn setTitle:@"使用" forState:UIControlStateNormal];
        isUseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        isUseBtn.tintColor = [UIColor blueColor];
        isUseBtn.backgroundColor = dockwhite_;
        isUseBtn.clipsToBounds = YES;
        isUseBtn.layer.cornerRadius = 8;
        [isUseBtn addTarget:self action:@selector(clickChanceIsUse:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:isUseBtn];
        [isUseArrM addObject:isUseBtn];
        
        if ([model.isUse isEqualToString:@"1"]) {
            isUseBtn.backgroundColor = [UIColor redColor];
            isUseBtn.selected = YES;
            //不是拍卖的才能交互
            if ([_fabuName isEqualToString:@"0"] || [_fabuName isEqualToString:@"2"]) {
                
                textFiled.userInteractionEnabled = YES;
                
            }
            textFiled.text = model.value;
        }else {
            isUseBtn.backgroundColor = dockwhite_;
            isUseBtn.selected = NO;
        }
        
    }
    
    return self;
    
}
- (void)setTitleText:(NSString *)titleText {
    
    _titleText = titleText;
    titleLab.text = [NSString stringWithFormat:@"%@:",titleText];
    
}
- (void)change:(UITextField *)Filed {
    
    if ([self.delegate respondsToSelector:@selector(wenBenattr_id:isDefault:isUse:changePrice:value:)]) {
        
        NSString *str;
        
        if (isUseBtn.selected == YES) {
            str = @"true";
        }else {
            str = @"false";
        }
        
        [self.delegate wenBenattr_id:self.attr_id isDefault:@"true" isUse:str changePrice:@"0.00" value:Filed.text];
    }
    
}
- (void)clickChanceIsUse:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        
        [btn setBackgroundColor:[UIColor redColor]];
        
        if ([_fabuName isEqualToString:@"0"] || [_fabuName isEqualToString:@"2"]) {
            
            textFiled.userInteractionEnabled = YES;
            
        }
        
        /*
         这个位置添加一条代理方法
         **/
        if ([self.delegate respondsToSelector:@selector(wenBenattr_id:isDefault:isUse:changePrice:value:)]) {
             
             [self.delegate wenBenattr_id:self.attr_id isDefault:@"true" isUse:@"true" changePrice:@"0.00" value:textFiled.text];
         }
        
    } else {
        
        [btn setBackgroundColor:dockwhite_];
        
        textFiled.userInteractionEnabled = NO;
        
        textFiled.text = nil;
    
        /*
         这个位置添加一条代理方法
         **/
        if ([self.delegate respondsToSelector:@selector(wenBenattr_id:isDefault:isUse:changePrice:value:)]) {
            
            [self.delegate wenBenattr_id:self.attr_id isDefault:@"true" isUse:@"false" changePrice:@"0.00" value:textFiled.text];
        }
        
    }
    
}

@end
